package ict;

import java.util.ArrayList;

import ict.mobile.R;
import android.app.Activity;
import android.os.Bundle;

import android.app.Activity;
import android.os.Bundle;
import android.database.Cursor;
import android.database.sqlite.*;
import android.view.*;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class updateQuantity extends Activity implements
AdapterView.OnItemSelectedListener{
	

	SQLiteDatabase db;
	String sql;
	Cursor cursor = null;
	String[] columns = { "mid", "name", "password", "age" };
	String dataStrHeader = String.format("%4s %-12s %-9s %3s\n", "Mid", "Name",
			"Password", "Age");
	String dataStr;
	EditText updateQuantityET;
	Button updateQuantityB;
	String pos;

	ArrayList IDList = new ArrayList();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.updatequantity);

		
		findViewBy();
		addProductCodeToList();
		
		Spinner spin=(Spinner)findViewById(R.id.productCodespinner);
		spin.setOnItemSelectedListener(this);
		ArrayAdapter<String> aa=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, IDList);
		aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin.setAdapter(aa);
	}

	// ////find view by ID
	public void findViewBy() {
		updateQuantityET = (EditText) findViewById(R.id.updateQuantityET);
		updateQuantityB = (Button) findViewById(R.id.updateQuantityB);
		final Spinner productCodespin = (Spinner) findViewById(R.id.productCodespinner);

	}

	public void onUpdate(View view) {
		updateQuantity(pos);
		finish();
		
	}

	// //Update
	public void updateQuantity(String x) {
		try {
			db = SQLiteDatabase.openDatabase("/data/data/ict.mobile/icsDB",
					null, SQLiteDatabase.OPEN_READWRITE);

			String sql = "Update Product set Quantity = "
					+ updateQuantityET.getText().toString()
					+ " where ProductCode = '" + x +"';";
			db.execSQL(sql);
			db.close();

		} catch (SQLiteException e) {
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
		}

	}

	// /add productCode to spinner
	public void addProductCodeToList() {
		try {
			db = SQLiteDatabase.openDatabase("/data/data/ict.mobile/icsDB",
					null, SQLiteDatabase.OPEN_READONLY);

			cursor = db.rawQuery("select * from Product ;", null);
			// Toast.makeText(this, "OK", Toast.LENGTH_LONG).show();

			while (cursor.moveToNext()) {
				// or cursor.getInt(0);
				String ProductCode = cursor.getString(0);
				String ProductName = cursor.getString(1);
				int Quantity = cursor.getInt(2);
				IDList.add(ProductCode);
			}

			

			//Toast.makeText(this, IDList.toString(), Toast.LENGTH_LONG).show();

			db.close();

		} catch (SQLiteException e) {
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
		}
	}

	// ///display Quantity
	public void selectAllFromProduct(String productC) {
		try {
			db = SQLiteDatabase.openDatabase("/data/data/ict.mobile/icsDB",
					null, SQLiteDatabase.OPEN_READONLY);

			//cursor = db.rawQuery("select * from Product where ProductCode = '" + productC + "'", null);
			cursor = db.rawQuery("select * from Product where ProductCode = '" + productC + "'", null);
			// Toast.makeText(this, "OK", Toast.LENGTH_LONG).show();

			while (cursor.moveToNext()) {
				// or cursor.getInt(0);
				String ProductCode = cursor.getString(0);
				String ProductName = cursor.getString(1);
				int Quantity = cursor.getInt(2);
				//IDList.add(ProductCode);
				dataStr = "";
				dataStr += Quantity;
			}

			// Toast.makeText(this, IDList.toString(),
			// Toast.LENGTH_LONG).show();
			updateQuantityET.setText(dataStr);

			db.close();

		} catch (SQLiteException e) {
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
		}

	}

	public void onItemSelected(AdapterView parent, View v, int position, long id){
		// TODO Auto-generated method stub
		selectAllFromProduct(""+IDList.get(position));
		pos = ""+IDList.get(position);
	}

	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}

	//

}